//
//  BHCircleClassShowView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/7.
//

import UIKit

protocol BHCircleClassShowViewDelegate {
    func didSelectColumn(model: BHCircleColumnModel, index: Int)
    func editeColumnComplete(array:[BHCircleColumnModel])
}

class BHCircleClassShowView: UIView {

    var delegate: BHCircleClassShowViewDelegate?
    
    
    let height_: CGFloat = 373
    
    @IBOutlet weak var content_view: UIView!
    
    @IBOutlet weak var editeBtn: UIButton!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var allColumnArray: [BHCircleColumnModel] = [] {
        didSet {
            self.tempAllColumnArray = allColumnArray
            self.collectionView.reloadData()
        }
    }
    
    var moreColumnArray: [BHCircleColumnModel] = [] {
        didSet {
            self.tempMoreColumnArray = moreColumnArray
            self.collectionView.reloadData()
        }
    }
    
    fileprivate var tempAllColumnArray: [BHCircleColumnModel] = []
    fileprivate var tempMoreColumnArray: [BHCircleColumnModel] = []

    var tempEditeArray: [BHCircleColumnModel] = []

    
    // 是否正在编辑
    fileprivate var editingType: Bool = false
    
    static func loadFromXib() -> BHCircleClassShowView {
        let viewArray = Bundle.main.loadNibNamed("BHCircleClassShowView", owner: nil, options: nil)
        let view: BHCircleClassShowView = viewArray![0] as! BHCircleClassShowView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.isHidden = true
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        self.editeBtn.addLayerCorner(12, 0.5, .color_cccccc)
        
        self.collectionView.regiseterNib(nibName: "BHCircleTextItemCollectionViewCell")
        self.collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "bh_sec_device_header")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
    }
    
    func show()  {
        self.isHidden = false
        
    }
    
    func hide()  {
        self.isHidden = true
        self.editingType = false

    }
    
    @IBAction func closeAct(_ sender: UIButton) {
        self.allColumnArray = self.tempAllColumnArray
        self.moreColumnArray = self.tempMoreColumnArray
        self.collectionView.reloadData()
        self.hide()
    }
    
    @IBAction func editeAct(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.editingType = sender.isSelected
        if self.editingType == false {
            self.delegate?.editeColumnComplete(array: self.tempEditeArray)
            self.isHidden = true
        }
        self.collectionView.reloadData()
    }
}
extension BHCircleClassShowView: UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return self.allColumnArray.count+self.tempEditeArray.count
        }
        return self.moreColumnArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let  width = (UIScreen.width-22)/4.0
        return CGSize(width: width-0.5, height: 46)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 1 {
            return CGSize(width: UIScreen.width, height: 50)
        }
        return .zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 11, bottom: 0, right: 11)
    }
}

extension BHCircleClassShowView: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader {
            if indexPath.section == 1 {
                let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "bh_sec_device_header", for: indexPath)
                var label = header.viewWithTag(200) as? UILabel
                if label == nil  {
                    label = UILabel.init(frame: CGRect(x: 16, y: 10, width: UIScreen.width, height: 40))
                    label?.font = .systemFont(ofSize: 20, weight: .bold)
                    label?.text = "更多栏目"
                    label?.textColor = .color_34CE98
                    label?.tag = 200
                    header.addSubview(label!)
                }
                
                return header
                
            }
        }
        return UICollectionReusableView()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCircleTextItemCollectionViewCell", for: indexPath) as! BHCircleTextItemCollectionViewCell
        cell.chose = false
        if indexPath.section == 0 {
            cell.edite = self.editingType
            if indexPath.row < self.allColumnArray.count {
                let model = self.allColumnArray[indexPath.row]
                cell.model = model
            }else {
                let model = self.tempEditeArray[indexPath.row-self.allColumnArray.count]
                model.fixedFlag = 0
                cell.model = model
                cell.del_btn.addBtnAction(action: {[weak self] (_) in
                    model.fixedFlag = 1
                    self?.tempEditeArray.remove(at: indexPath.row-(self?.allColumnArray.count ?? 0))
                    self?.moreColumnArray.append(model)
                }, for: .touchUpInside)
            }
            
        }else {
            cell.edite = false
            let model = self.moreColumnArray[indexPath.row]
            cell.model = model
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if self.editingType == true {
                return
            }
            if indexPath.row < self.allColumnArray.count {
                let model = self.allColumnArray[indexPath.row]
                self.delegate?.didSelectColumn(model: model, index: indexPath.row)
            }else {
                let model = self.tempEditeArray[indexPath.row-self.allColumnArray.count]
                self.delegate?.didSelectColumn(model: model, index: indexPath.row)
            }
            self.hide()
        }else {
            if self.editingType == false {
                return
            }
            
            let model = self.moreColumnArray[indexPath.row]
            self.moreColumnArray.remove(at: indexPath.row)
            self.tempEditeArray.append(model)
        }
        
        collectionView.reloadData()
    }
}
